package net.phoenix.repository.value;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Calendar;

import javax.jcr.Binary;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public abstract class BasicValue implements Value, Comparable<Value> {
	public BasicValue() {
	}

	public int getType() {
		return PropertyType.UNDEFINED;
	}

	public String getString() throws RepositoryException {
		return null;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getStream()
	 */
	@Override
	public InputStream getStream() throws ValueFormatException, RepositoryException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Stream.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getLong()
	 */
	@Override
	public long getLong() throws ValueFormatException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Long");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getDouble()
	 */
	@Override
	public double getDouble() throws ValueFormatException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Double");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getDate()
	 */
	@Override
	public Calendar getDate() throws ValueFormatException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Date.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getBoolean()
	 */
	@Override
	public boolean getBoolean() throws ValueFormatException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Boolean");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getBinary()
	 */
	@Override
	public Binary getBinary() throws RepositoryException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Binary");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.jcr.Value#getDecimal()
	 */
	@Override
	public BigDecimal getDecimal() throws ValueFormatException, RepositoryException {
		throw new ValueFormatException("Can not convert from " + PropertyType.nameFromValue(getType()) + " to Decimal");
	}

	

	public String toString() {
		try {
			return this.getString();
		} catch (RepositoryException e) {
			return super.toString();
		}
	}

}
